package com.sdgakj.repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

public class CommonReviewDaoImpl {

	@PersistenceContext
	private EntityManager em;
	
	public List<Object[]> findReviewByFeedId(Integer type,Long feedId){
		StringBuffer sql =new StringBuffer();
		sql.append("SELECT r.note,r.create_date,u.loginname as operator,c.content as mcontent, \n");
		sql.append(" c1.user_id,u1.loginname as mlogin \n");
		sql.append(" from sht_review r \n");
		sql.append(" INNER JOIN sht_feed_comment c on r.target_id =c.id and r.type=:type AND c.feed_id=:feedId \n");
		sql.append(" LEFT JOIN sht_user u on r.user_id =u.id \n");
		sql.append(" LEFT JOIN sht_customor c1 on c.customer_id =c1.id \n"); 
		sql.append(" LEFT JOIN sht_user u1 on c1.user_id =u1.id \n");
		sql.append(" ORDER BY r.id DESC \n");
		Query q =em.createNativeQuery(sql.toString());
		if(type != null){
			q.setParameter("type", type);
		}
		if(feedId !=null){
			q.setParameter("feedId", feedId);
		}
		return q.getResultList();
	}

}
